using UnityEngine;
using System.Collections.Generic;
using System.IO;
using UnityEngine.UI;

public class LoadImage : MonoBehaviour
{


	//轮播图列表
	[HideInInspector]
	public static List<Sprite> Btn1SPs = new List<Sprite>(), Btn2SPs = new List<Sprite>(), Btn3SPs = new List<Sprite>(), Btn4SPs = new List<Sprite>(),
							   Btn5SPs = new List<Sprite>(), Btn6SPs = new List<Sprite>(), Btn7SPs = new List<Sprite>(), Btn8SPs = new List<Sprite>(),
							   Btn9SPs = new List<Sprite>(), Btn10SPs = new List<Sprite>(), Btn11SPs = new List<Sprite>(), Btn12SPs = new List<Sprite>(),
							   Btn13SPs = new List<Sprite>(), Btn14SPs = new List<Sprite>(), Btn15SPs = new List<Sprite>(), Btn16SPs = new List<Sprite>(), 
							   Btn17SPs = new List<Sprite>(), Btn18SPs = new List<Sprite>(), Btn19SPs = new List<Sprite>(), Btn20SPs = new List<Sprite>(),
							   Btn21SPs = new List<Sprite>(), Btn22SPs = new List<Sprite>(), Btn23SPs = new List<Sprite>(), Btn24SPs = new List<Sprite>(),
							   Btn25SPs = new List<Sprite>(), Btn26SPs = new List<Sprite>(), Btn27SPs = new List<Sprite>(), Btn28SPs = new List<Sprite>();
    private void Awake()
	{
		/*LoadFileALLTexture("Images/生态环境目录按钮", DxxmPageMgr.GetInstance.DxxmBtnTexture2Ds);
		LoadFileALLTexture("Images/详情页内容图", ContentPageMgr.GetInstance.TextTexture2Ds);


		//轮播图列表
		LoadFileALLTexture("Images/详情页轮播图/按钮轮播图 1", Btn1SPs);
		LoadFileALLTexture("Images/详情页轮播图/按钮轮播图 2", Btn2SPs);
		LoadFileALLTexture("Images/详情页轮播图/按钮轮播图 3", Btn3SPs);
		LoadFileALLTexture("Images/详情页轮播图/按钮轮播图 4", Btn4SPs);
		LoadFileALLTexture("Images/详情页轮播图/按钮轮播图 5", Btn5SPs);
		LoadFileALLTexture("Images/详情页轮播图/按钮轮播图 6", Btn6SPs);
		LoadFileALLTexture("Images/详情页轮播图/按钮轮播图 7", Btn7SPs);
		LoadFileALLTexture("Images/详情页轮播图/按钮轮播图 8", Btn8SPs);
		LoadFileALLTexture("Images/详情页轮播图/按钮轮播图 9", Btn9SPs);
		LoadFileALLTexture("Images/详情页轮播图/按钮轮播图 10", Btn10SPs);
		LoadFileALLTexture("Images/详情页轮播图/按钮轮播图 11", Btn11SPs);
		LoadFileALLTexture("Images/详情页轮播图/按钮轮播图 12", Btn12SPs);
		LoadFileALLTexture("Images/详情页轮播图/按钮轮播图 13", Btn13SPs);
		LoadFileALLTexture("Images/详情页轮播图/按钮轮播图 14", Btn14SPs);
		LoadFileALLTexture("Images/详情页轮播图/按钮轮播图 15", Btn15SPs);
		LoadFileALLTexture("Images/详情页轮播图/按钮轮播图 16", Btn16SPs);
		LoadFileALLTexture("Images/详情页轮播图/按钮轮播图 17", Btn17SPs);
		LoadFileALLTexture("Images/详情页轮播图/按钮轮播图 18", Btn18SPs);
		LoadFileALLTexture("Images/详情页轮播图/按钮轮播图 19", Btn19SPs);
		LoadFileALLTexture("Images/详情页轮播图/按钮轮播图 20", Btn20SPs);
        LoadFileALLTexture("Images/详情页轮播图/按钮轮播图 21", Btn21SPs);
        LoadFileALLTexture("Images/详情页轮播图/按钮轮播图 22", Btn22SPs);
        LoadFileALLTexture("Images/详情页轮播图/按钮轮播图 23", Btn23SPs);
        LoadFileALLTexture("Images/详情页轮播图/按钮轮播图 24", Btn24SPs);
        LoadFileALLTexture("Images/详情页轮播图/按钮轮播图 25", Btn25SPs);
        LoadFileALLTexture("Images/详情页轮播图/按钮轮播图 26", Btn26SPs);
        LoadFileALLTexture("Images/详情页轮播图/按钮轮播图 27", Btn27SPs);
        LoadFileALLTexture("Images/详情页轮播图/按钮轮播图 28", Btn28SPs);*/

    }


	void LoadFileALLTexture(string FilleName, List<Sprite> texture2Ds )
	{
		List<string> filePaths = new List<string>();
		string imgtype = "*.BMP|*.JPG|*.GIF|*.PNG";
		string[] ImageType = imgtype.Split('|');
		for (int i = 0; i < ImageType.Length; i++)
		{
			//获取d盘中a文件夹下所有的图片路径
			string[] dirs = Directory.GetFiles(Application.streamingAssetsPath + "/"+ FilleName, ImageType[i]);
			for (int j = 0; j < dirs.Length; j++)
			{
				filePaths.Add(dirs[j]);
			}
		}

	
		for (int i = 0; i < filePaths.Count; i++)
		{
			
			Texture2D tx = new Texture2D(100, 100) ;
			tx.LoadImage(getImageByte(filePaths[i]));
            Sprite _sprite = Sprite.Create(tx, new Rect(0, 0, tx.width, tx.height), Vector3.zero);
            texture2Ds.Add(_sprite);

        }
    }

	public static void LoadFileTexture(string path)
    {

    }

	/// <summary>
	/// 加载一张图片到物体身上
	/// </summary>
	/// <param name="path">路径</param>
	/// <param name="game">要加载到哪个对象身上去</param>
	public static void LoadFileImage(string path, GameObject game)
	{
		string streamingAssetsPath;
		string imgtype = ".BMP|.JPG|.GIF|.PNG";
		string[] ImageType = imgtype.Split('|');
		for (int i = 0; i < ImageType.Length; i++)
		{
			//获取streamingAssetsPath的实际路径
			streamingAssetsPath = Path.Combine(Application.streamingAssetsPath, path+ImageType[i]);
			if (File.Exists(streamingAssetsPath))
			{
				//加载图片为字节数组bytel]
				byte[] imageBytes = File.ReadAllBytes(streamingAssetsPath);
				Texture2D texture = new Texture2D(2, 2);// 临时尺寸，稍后会被替换
				texture.LoadImage(imageBytes);//加载图片数据

				// 获取图片的原始大小  
				int width = texture.width;
				int height = texture.height;

				// 注意:由于Texture2D.LoadImage()可能不保留原始图片的宽高比和尺寸,
				// 你可能需要调整texture的尺寸来匹配原始图片。
				//这里我们假设图片的尺寸是正确的，或者你已经根据需要调整了texture的尺寸

				//创建sprite并分配给按钮
				Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(8.5f, 8.5f));
				if (game != null)
				{
					game.GetComponent<Image>().sprite = sprite;
					// 根据图片大小设置按钮大小  
					Vector2 buttonSize = new Vector2(width, height);
					RectTransform buttonRectTransform = game.GetComponent<RectTransform>();
					buttonRectTransform.sizeDelta = buttonSize;
				}
				else
				{
					Debug.LogError("物体不存在!");
				}
			}
			//else
			//{
			//	Debug.LogError("图片文件不存在:" + streamingAssetsPath);
			//}
		}
	}



	/// <summary>
	/// 根据图片路径返回图片的字节流byte[]
	/// </summary>
	/// <param name="imagePath">图片路径</param>
	/// <returns>返回的字节流</returns>
	private static byte[] getImageByte(string imagePath)
	{
		FileStream files = new FileStream(imagePath, FileMode.Open);
		byte[] imgByte = new byte[files.Length];
		files.Read(imgByte, 0, imgByte.Length);
		files.Close();
		return imgByte;
	}

	


	/// <summary>
	/// 单张图片加载 返回Sprite
	/// </summary>
	/// <param name="imagePath">"/Image/0.JPG"</param>
	/// <returns></returns>
	public static Sprite LoadSprite(string imagePath)
	{

		string _path = Application.streamingAssetsPath + imagePath;//获取地址

		FileStream _fileStream = new FileStream(_path, FileMode.Open, FileAccess.Read);//使用流数据读取

		byte[] _buffur = new byte[_fileStream.Length];

		_fileStream.Read(_buffur, 0, _buffur.Length);//转换成字节流数据

		Texture2D _texture2d = new Texture2D(10, 10);//设置宽高

		_texture2d.LoadImage(_buffur);//将流数据转换成Texture2D

		Sprite _sprite = Sprite.Create(_texture2d, new Rect(0, 0, _texture2d.width, _texture2d.height), Vector3.zero);//创建一个Sprite


		//image.sprite = _sprite;	//赋值
		return _sprite;

	}

	/// <summary>
	/// 单张图片加载,返回Texture2D
	/// </summary>
	/// <param name="imagePath">"/Image/0.JPG"</param>
	/// <returns></returns>
	public static Texture2D LoadTexture(string imagePath)
	{

		string _path = Application.streamingAssetsPath + imagePath;//获取地址

		FileStream _fileStream = new FileStream(_path, FileMode.Open, FileAccess.Read);//使用流数据读取

		byte[] _buffur = new byte[_fileStream.Length];

		_fileStream.Read(_buffur, 0, _buffur.Length);//转换成字节流数据

		Texture2D _texture2d = new Texture2D(10, 10);//设置宽高

		_texture2d.LoadImage(_buffur);//将流数据转换成Texture2D

		//rawImage.texture = _texture2d;	 //赋值

		return _texture2d;

	}


	//ResourcesLoadImg("切图/切图/家乡荣耀内容图", 家乡荣耀Textures);
	public void ResourcesLoadImg(string path, List<Texture2D> texture2Ds)
	{
		Object[] Objs = Resources.LoadAll(path, typeof(Texture2D));
		foreach (var item in Objs)
		{

			Texture2D texture2D = item as Texture2D;
			texture2Ds.Add(texture2D);
		}
	}


	//		ShowLoadImage(家乡荣耀Textures, 家乡荣耀ContentTr);
	public void ShowLoadImage(List<Texture2D> allTex2d, RectTransform ContentTr)
	{

		ContentTr.anchoredPosition = Vector2.zero;
		if (allTex2d.Count != 0)
		{
			//把加载的图片显示出来
			for (int i = 0; i < allTex2d.Count; i++)
			{
				//Sprite s = Sprite.Create(TexList[i], new Rect(0, 0, TexList[i].width, TexList[i].height), Vector2.zero);
				GameObject go = new GameObject();
				RawImage im = go.AddComponent<RawImage>();
				im.texture = allTex2d[i];
				go.transform.SetParent(ContentTr);
				go.GetComponent<RectTransform>().sizeDelta = new Vector2(1920, 1080);

			}

		}
		ContentTr.GetComponent<RectTransform>().sizeDelta = new Vector2(1920f * (allTex2d.Count - 1), 0);

	}

}